package com.task.myapplication12;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

//加背景音乐，视频学习，手机拍照加到ImageView中
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback{
    private MediaPlayer mediaPlayer1;
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private Button btnPlay,btnStop,btnPause;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MediaPlayer mediaPlayer=MediaPlayer.create(this,R.raw.music);
        mediaPlayer1=new MediaPlayer();
        mediaPlayer.start();
        btnPlay=findViewById(R.id.btnStart);
        btnPause=findViewById(R.id.btnPause);
        btnStop=findViewById(R.id.btnStop);
        surfaceView=findViewById(R.id.mySurface);
        surfaceHolder=surfaceView.getHolder();
        surfaceHolder.setKeepScreenOn(true);
        surfaceHolder.setFixedSize(320,220);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceHolder.addCallback(this);
        btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                play();
            }
        });
        btnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer1.pause();
            }
        });
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer1.stop();
            }
        });
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        if(mediaPlayer1.isPlaying()){
            mediaPlayer1.stop();
        }
        mediaPlayer1.release();
    }
    public void play(){
        try{
            mediaPlayer1.reset();
            mediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);
            //Toast.makeText(this,Environment.getExternalStorageDirectory().getPath() , Toast.LENGTH_SHORT).show();
            Uri uri=Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/test.mp4");
            mediaPlayer1.setDataSource(MainActivity.this,uri);
            mediaPlayer1.setDisplay(surfaceView.getHolder());
            mediaPlayer1.prepare();
            mediaPlayer1.start();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        try {
            play();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
    }
    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
    }
}